{extend name="public:base" /}
{block name="content"}
<div class="page-header">
	<h1>
		{$breadcrumb1}
		<small>
			<i class="ace-icon fa fa-angle-double-right"></i>
			{$breadcrumb2}
		</small>
		<small>
			<i class="ace-icon fa fa-angle-double-right"></i>
			{$crumbs}
		</small>			
	<button id="send" form="form-goods" type="submit" style="float:right;"  class="btn btn-sm btn-primary">提交</button>	
	</h1>
</div>

<div class="row">
	<div class="col-xs-12">	
		
		<div class="panel-body">
        <form action="{$action}" method="post" enctype="multipart/form-data" id="form-goods" class="form-horizontal">
	        	<ul class="nav nav-tabs">          	
		          	<li class="active"><a href="#tab-data" data-toggle="tab">基本信息</a></li>          	
		            <li><a href="#tab-general" data-toggle="tab">详情描述</a></li>      
		            <li><a href="#tab-link" data-toggle="tab">关联</a></li>         
	            </ul>
	            <div class="tab-content">
	            	
	            	<div class="tab-pane active" id="tab-data">
	            		{if condition="$Request.param.id"}
						<input name="id" type="hidden" value="{$Request.param.id}" />
						{/if}
						
						
						<input name="goods_name" type="hidden" value="{$d.goods_name|default=''}" />
						
						<div class="form-group">
			                <label class="col-sm-1 control-label" for="input-image">
			                	<span title="" data-toggle="tooltip" data-original-title="上传800x800的图片">团购主图</span>
			                </label>	                
				            <div class="col-sm-10" id="thumb">
				                  <a id="thumb-image" href="#" data-toggle="image" class="img-thumbnail">
					                  	{if condition="!empty($d['group_image'])"}
					                  		<img src="IMG_ROOT{$d.group_image|resize=100,100}" />
					                  	{else /}
					                  		<img src="__PUBLIC__/image/no_image_100x100.jpg" />
					                  	{/if}
										</a>
				                  <input type="hidden" name="group_image" value="{$d.group_image|default=''}" id="input-image" />
				            </div>		            
					    </div>
						
						<div class="form-group">
							<label class="col-sm-1 control-label no-padding-left"> * 团购名称 </label>
						<div class="col-sm-11">
							<div class="clearfix">
							<input name="group_name" id="group_name" class="col-xs-10 col-sm-5"  value="{$d.group_name|default=''}" type="text">
							</div>
						</div>
						</div>
						<div class="space-4"></div>
						
						<div class="form-group">							
							<label class="col-sm-1 control-label">
			                	*原价
			                </label>
						<div class="col-sm-11">
							<div class="clearfix">
							
							<input name="price" type="text" value="{$d.goods_price|default=''}" class="col-xs-10 col-sm-5" />
							</div>
						</div>
						</div>
						
						<div class="form-group">							
							<label class="col-sm-1 control-label">
			                	<span title="" data-toggle="tooltip" data-original-title="团购价格应包含邮费，团购商品系统默认不收取邮费">*团购价</span>
			                </label>
						<div class="col-sm-11">
							<div class="clearfix">
							<input name="group_price" id="group_price" class="col-xs-10 col-sm-5" value="{$d.group_price|default=''}" type="text">
							</div>
						</div>
						</div>
						<div class="space-4"></div>			
						
						<div class="form-group">
							<label class="col-sm-1 control-label" for="input-image">
			                	商品数量
			                </label>
				
						<div class="col-sm-11">
							<div class="clearfix">
							<input name="quantity" id="quantity" class="col-xs-10 col-sm-5" value="{$d.quantity|default=''}" type="text">
							</div>
						</div>
						</div>
						<div class="space-4"></div>	
						
						<div class="form-group">
							<label class="col-sm-1 control-label" for="input-image">
			                	<span title="" data-toggle="tooltip" data-original-title="每个买家ID可团购的最大数量，不限数量请填0">限购数量</span>
			                </label>
				
						<div class="col-sm-11">
							<div class="clearfix">
							<input name="buy_limit" id="buy_limit" class="col-xs-10 col-sm-5" value="{$d.buy_limit|default=''}" type="text">
							</div>
						</div>
						</div>
						<div class="space-4"></div>	
						
						<div class="form-group required input-daterange">
							<label class="col-sm-1 control-label" for="input-name2">时间</label>
							<div class="col-sm-3">
								<div class="input-daterange input-group">
									<input class="input-sm form-control" type="text" name="start_time" value="<?php if(isset($d)&&$d['start_time']!=0){echo date('Y-m-d',$d['start_time']);} ?>">
									<span class="input-group-addon">
									<i class="fa fa-arrow-right"></i>
									</span>
									<input class="input-sm form-control" type="text" name="end_time" value="<?php if(isset($d)&&$d['end_time']!=0){echo date('Y-m-d',$d['end_time']);} ?>">
								</div>
							</div>
						</div>
						
							
						
						<div class="form-group">
							<label class="col-sm-1 control-label">状态</label>
							<div class="col-sm-11">
								<label class="radio-inline">
								<input type="radio" checked="checked" value="1" name="status">开启</label>
								<label class="radio-inline">
								<input type="radio" value="0" name="status">关闭</label>
							</div>
						</div>	
						
	            	</div>
	            		
	            		
	            	<div class="tab-pane" id="tab-general">
		          			<div class="table-responsive">
			                	<table id="images" class="table table-striped table-bordered table-hover">
				                  <thead>
				                    <tr>
				                      <td class="text-left">详情图片</td>
				                      <td class="text-left">图片描述</td>
				                      <td class="text-right">选项排序</td>
				                      <td></td>
				                    </tr>
				                  </thead>
				                	 <tbody>
				                	 	<?php $image_row = 0; ?>
				                	 	<?php if(isset($image)){ ?>
				                	 		<?php foreach ($image as $goods_image) { ?>
						                	 	<tr id="des-image-row{$image_row}">
													<td class="text-left"><a href="#" id="des-thumb-image{$image_row}"data-toggle="image" class="img-thumbnail"><img src="/<?php echo resize($goods_image['image'],100,100); ?>" /></a><input type="hidden" name="image[{$image_row}][image]" value="{$goods_image.image}" id="des-input-image{$image_row}" /></td>
													<td class="text-right"><input type="text" name="image[{$image_row}][description]" value="{$goods_image.alt}" class="form-control" /></td>
													<td class="text-right"><input type="text" name="image[{$image_row}][sort_order]" value="{$goods_image.sort_order}"  size="1" class="form-control" /></td>
													<td class="text-left"><button type="button" onclick="$('#des-image-row{$image_row}').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="fa fa-trash"></i></button></td>
												</tr>
											<?php $image_row++; ?>
						                    <?php } ?>
				                	 	<?php } ?>
				                	 </tbody>
					              </table>  					          
				               	  <div><a  onclick="add_image();" class="add_image btn btn-primary ">添加图片</a></div>
		                  	</div>
		          	</div>	
	            	
	            	<div class="tab-pane" id="tab-link">
	            		<div class="form-group">
			                <label class="col-sm-1 control-label" for="input-category"><span data-toggle="tooltip" title="(自动完成)">关联商品</span></label>
			                <div class="col-sm-11">
			                  <input type="text" name="goods" value="" placeholder="关联商品" id="input-goods" class="form-control" />
			                  <div id="goods-select" class="well well-sm" style="height: 150px; overflow: auto;">
			                  	{if condition="isset($d) and $d['goods_id']"}
					                    <div id="goods-id{$d.goods_id}"><i class="fa fa-minus-circle"></i>{$d.goods_name}<input type="hidden" name="goods_id[]" value="{$d.goods_id}" /></div>
				              	{/if}
			                  </div>	                  
			                </div>
	              		</div>	
						
						<div class="form-group">					
							<label class="col-sm-1 control-label">
			                	<span title="" data-toggle="tooltip" data-original-title="选择是，不需要上传详情图片，会使用关联商品的详情">使用关联商品详情</span>
			                </label>
							<div class="col-sm-11">
								<label class="radio-inline">
								<input type="radio" checked="checked" value="1" name="use_goods_des">是</label>
								<label class="radio-inline">
								<input type="radio" value="0" name="use_goods_des">否</label>
							</div>
						</div>
	            	</div>
	            		
	            </div>
        </form>
        </div>
		
	</div>
</div>
{/block}
{block name="javascript"}
 <link rel="stylesheet" href="__ADMIN__/ace/css/datepicker.min.css" />

<link rel="stylesheet" href="__ADMIN__/ace/css/daterangepicker.min.css" />


<script src="__ADMIN__/ace/js/moment.min.js"></script>
<script src="__ADMIN__/ace/js/bootstrap-datepicker.min.js"></script>

<script src="__ADMIN__/ace/js/bootstrap-datepicker.zh-CN.js"></script>

<script>

$('.input-daterange').datepicker({
            language: 'zh-CN',
            autoclose: true,
            todayHighlight: true,
            format:'yyyy-mm-dd'
});

$('input[name=\'goods\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: '{:url("Goods/autocomplete")}'+'/type/2'+'/filter_name/'+  encodeURIComponent(request),
			dataType: 'json',			
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['goods_id'],
						price: item['price']
					}
				}));
			}
		});
	},
	'select': function(item) {
		
		$('input[name=\'price\']').val(item['price']);
		$('input[name=\'goods_name\']').val(item['label']);
		
		$('#goods-select' + item['value']).remove();
		
		$('#goods-select').html('<div id="goods-id' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="goods_id[]" value="' + item['value'] + '" /></div>');	
	}
});	

$('#goods-select').delegate('.fa-minus-circle', 'click', function() {
	$(this).parent().remove();
});	

var image_row =1;

function add_image() {
	html  = '<tr id="des-image-row' + image_row + '">';
	html += '  <td class="text-left"><a href="#" id="des-thumb-image' + image_row + '"data-toggle="image" class="img-thumbnail"><img src="__PUBLIC__/image/no_image_100x100.jpg" /><input type="hidden" name="image[' + image_row + '][image]" value="" id="des-input-image' + image_row + '" /></td>';
	html += '<td class="text-right"><input type="text" name="image['+image_row+'][description]" value="" class="form-control" /></td>';
	html += '  <td class="text-right"><input type="text" name="image[' + image_row + '][sort_order]" value="'+image_row+'"  size="1" class="form-control" /></td>';
	html += '  <td class="text-left"><button type="button" onclick="$(\'#des-image-row' + image_row  + '\').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="fa fa-trash"></i></button></td>';
	html += '</tr>';
	
	$('#images tbody').append(html);
	
	image_row++;
}

$('#send').click(function(){
	
	var title=$('input[name="group_name"]').val();
	var group_price=$('input[name="group_price"]').val();
	
	var price=$('input[name="price"]').val();
	var quantity=$('input[name="quantity"]').val();
	//var goods_id=$('#goods-select').html();
	
	if(title==''){
		alert('团购名称必填');
		return false;
	}
	if(group_price==''){
		alert('团购价格必填');
		return false;
	}
	if(price==''){
		alert('原价必填');
		return false;
	}
	if(quantity==''){
		alert('数量必填');
		return false;
	}
	// if($.trim(goods_id)==''){
		// alert('关联商品必填');
		// return false;
	// }
	
	return true;
});    

$(function(){	

Oscshop.setValue("status", {$d.status|default=1});
Oscshop.setValue("use_goods_des", {$d.use_goods_des|default=0});
});   
</script>
{/block}
